package com.kgc.community.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.community.mapper.RoomUserMapper;
import com.kgc.community.mapper.RoomsMapper;
import com.kgc.community.mapper.UserMapper;
import com.kgc.community.pojo.*;
import com.kgc.community.service.PersonnelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dingChangXiao
 * @create 2020-11-26  17:16  com.kgc.community.service.impl  null.java
 */

@Service
public class PersonnelServiceImpl implements PersonnelService {
    @Resource
    UserMapper userMapper;

    @Resource
    RoomsMapper roomsMapper;

    @Resource
    RoomUserMapper roomUserMapper;

    @Override
    public PageInfo<RoomUser> selectAllPersonnel(String s, Integer pageNum, Integer pageSize) {
        List<User> users  = new ArrayList<>();
        List<Rooms> rooms = new ArrayList<>();
        if(!s.equals("%%")){
            UserExample example=new UserExample();
            example.createCriteria().andNameLike(s);
            users = userMapper.selectByExample(example);

            RoomsExample example1=new RoomsExample();
            example1.createCriteria().andIdLike(s);
            rooms = roomsMapper.selectByExample(example1);
        }

        //设置当前页和页容量，顺序不能改变
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");

        RoomUserExample example2=new RoomUserExample();
        if(!s.equals("%%")){
            if(users!=null &users.size()>0){
                List<Integer> ids = new ArrayList<>();
                for(int i=0;i<users.size();i++){
                    ids.add(users.get(i).getId());
               }
                    example2.createCriteria().andUserIdIn(ids);

            }
            if(users==null || users.size()==0){
                users=new ArrayList<>();

                User user = new User();
                 user.setId(-1);
                 example2.createCriteria().andUserIdEqualTo(user.getId());

            }
            RoomUserExample.Criteria criteria2 = example2.createCriteria();
            if(rooms!=null & rooms.size()>0){
                List<String> ids = new ArrayList<>();
                for(int j=0;j<rooms.size();j++){
                    ids.add(rooms.get(j).getId());
                }
                criteria2.andRoomIdIn(ids);
            }
            if(rooms==null||rooms.size()==0){
                rooms=new ArrayList<>();
                 Rooms  room = new Rooms();
                 room.setId("-1");
                 criteria2.andRoomIdEqualTo(room.getId());

            }
                example2.or(criteria2);
        }
        List<RoomUser> roomUsers = roomUserMapper.selectByExample(example2);
        PageInfo<RoomUser> pageInfo=new PageInfo<>(roomUsers);
        return pageInfo;

    }

    @Override
    public int deletePersonnel(Integer id) {
        Integer count = roomUserMapper.deleteByPrimaryKey(id);
        return count;
    }

    @Override
    public List<Rooms> selectRoomsId() {
        List<Rooms> rooms = roomsMapper.selectByExample(null);
        return rooms;
    }

    @Override
    public int addPersonnel(User user, RoomUser roomUser,Rooms room) {
        Integer count1 = userMapper.insertSelective(user);
        Integer count2 = roomUserMapper.insertSelective(roomUser);
        Integer count3 = roomsMapper.updateByPrimaryKeySelective(room);
        Integer count = 0;
        if(count1>0 &count2>0 &count3>0){
            count=1;
        }
        return count;
    }

    @Override
    public RoomUser selectPersonnel(Integer roomUserId) {
        RoomUserExample example=new RoomUserExample();
        RoomUserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(roomUserId);
        List<RoomUser> roomUsers = roomUserMapper.selectByExample(example);
        if(roomUsers!=null){
            for(RoomUser roomUser:roomUsers){
               return roomUser;
            }
        }
        return null;

    }

    @Override
    public int modifyPersonnel(User user, RoomUser roomUser) {
        roomUser.setUser(user);
        int count1 = userMapper.updateByPrimaryKeySelective(user);
        int count2 = roomUserMapper.updateByPrimaryKeySelective(roomUser);
        Integer count = 0;
        if(count1>0 &count2>0){
            count=1;
        }
        return count;
    }
}
